package com.resolution.dietaryassessment.listener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.view.View;

import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.utils.FragmentNotify;
import com.resolution.dietaryassessment.view.chronic.ChronicDiseaseActivity;
import com.resolution.dietaryassessment.view.chronic.DiabetesSubjectActivity;
import com.resolution.dietaryassessment.view.main.MainActivity;
import com.resolution.dietaryassessment.view.mydietary.BrushIDCardActivity;
import com.resolution.dietaryassessment.view.pagoda.DietaryPagodaActivity;
import com.resolution.dietaryassessment.view.tcm.BrushIDTCMActivity;
import com.resolution.dietaryassessment.view.tcm.TCMConstitutionActivity;

import java.io.Serializable;
import es.dmoral.toasty.Toasty;


/**
 * @author By Resolusion
 * @Title:
 * @Date: 2020/3/25
 * @Time: 10:10
 * @email Res01ution@foxmail.com
 */
public class MyListener implements View.OnClickListener, Serializable {

    private Context context;

    @SuppressLint("StaticFieldLeak")
    private static MyListener mMyListener = null;
    private int FLAG = NO_CHOICE;

    public final static int NO_CHOICE = 0;
    public final static int TRUE_CHOICE = 1;
    public final static int FALSE_CHOICE = 2;

    private static FragmentNotify fragmentNotify;

    public MyListener(Context context){
        this.context = context;
    }

    public static MyListener getInstance(Context context){
        fragmentNotify = (FragmentNotify)context;
        if (mMyListener == null){
            synchronized (MyListener.class){
                if (mMyListener == null){
                    mMyListener = new MyListener(context);
                }
            }
        }
        return mMyListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dietary_pagoda :
                start(DietaryPagodaActivity.class);
                break;
            case R.id.tcm_constitution :
                start(BrushIDTCMActivity.class);
                break;
            case R.id.chronic_disease :
                start(ChronicDiseaseActivity.class);
                break;
            case R.id.my_dietary :
                start(BrushIDCardActivity.class);
                break;
            case R.id.tcm_btn :
                start(TCMConstitutionActivity.class);
                break;
            case R.id.leave_btn :
                start(MainActivity.class);
                break;
            case R.id.star_btn:
                start(DiabetesSubjectActivity.class);
                break;
            case R.id.next_subject:
                if (getFLAG() == TRUE_CHOICE){
                    Toasty.success(context,"回答正确").show();
                }else if (getFLAG() == FALSE_CHOICE){
                    Toasty.error(context,"回答错误").show();
                }else if (getFLAG() == NO_CHOICE){
                    Toasty.error(context,"请选择答案后再点击按钮！").show();
                }
                fragmentNotify.FragNotifyCation(getFLAG());
            default:
        }
    }

    private int getFLAG(){
        return FLAG;
    }

    public void setFLAG(int flag){
        this.FLAG = flag;
    }

    private void start(Class<?> clazz){
        Intent intent = new Intent(context,clazz);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
